#include "head.h"
#include "func.h"
int recv_file_mmap(int fd_socket, char *filename, off_t filesize)
{
    int ret = -1;
    int fd = open(filename, O_RDWR | O_CREAT| O_TRUNC, 0666);
    ERROR_CHECK(fd, -1, "open");
    ret = ftruncate(fd, filesize);
    ERROR_CHECK(ret, -1, "ftruncate");
    printf("recv start...\n");
    char *pMap = (char*)mmap(NULL, filesize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
    ERROR_CHECK(pMap, NULL, "mmap");
    ret = recv(fd_socket, pMap, filesize, MSG_WAITALL);
    if (0 == ret) {
        //对端异常断开
        munmap(pMap, filesize);
        close(fd);
        return -2;
    }

    munmap(pMap, filesize);
    printf("recv finish!\n");

    //关闭文件
    close(fd);
    return 0;
}

